
import React from 'react';
import {observer } from 'mobx-react';
import {WeaDialog,WeaRightMenu,WeaNewScroll} from "ecCom"
import {Button,Modal} from "antd"
import {toJS} from "mobx"
import CptInfo from './cptInfo'
import Cptmend from "../public/cptMend"
import CptModifyLogDialog from '../dialog/CptModifyLogDialog'
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@observer
export default class CptEdit extends React.Component{
    componentWillReceiveProps(nextProps){
    }
    render(){
        const {contentStore,isdata,callBack } = this.props;
        const {showEditDialog,cptForm,cptInfo,setValidate} = this.props.contentStore;
        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@6yvlme`}  
                title={isdata == "2" ?getLabel(83598,"编辑资产") :getLabel(83597,"编辑资产资料")}
                visible={showEditDialog}
                buttons={this.getBottomButtons()}
                icon="icon-coms-fa"
                iconBgcolor="#f14a2d"
                onCancel={()=>{contentStore.showCptInfo(false)}}
                style={{width:1000,height:document.documentElement.clientHeight*0.8}}>
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@qzs2wz`} datas={this.getRightMenu()} onClick={this.onRightMenuClick.bind(this)}  >
                        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@kzk0t9`} height={"100%"}>
                            {showEditDialog && [<CptInfo ecId={`${this && this.props && this.props.ecId || ''}_CptInfo@zvfv2m@editDialog`} form={cptForm} condition={cptInfo} setValidate={setValidate} />,<Cptmend ecId={`${this && this.props && this.props.ecId || ''}_Cptmend@o8jdyt`} callBack={callBack} contentStore ={contentStore}/>]}
                        </WeaNewScroll>
                        <CptModifyLogDialog ecId={`${this && this.props && this.props.ecId || ''}_CptModifyLogDialog@phmj48`} store={contentStore} />
                    </WeaRightMenu>
            </WeaDialog>
        )
    }
    getBottomButtons(){
        const {contentStore} = this.props;
        const {cptInfo,saveCptInfo,capitalid,showCptInfo} = this.props.contentStore;
        let btnArr = []; 
        let rightMenu = toJS(cptInfo.rightMenus) || [];
        rightMenu && rightMenu.length>0  && rightMenu.map(m=>{
            m.isTop == '1' && btnArr.length < 4 && btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ii21al@${m.type}`} type="primary" 
                    onClick={()=>{
                        if(m.type == "BTN_SUBMIT"){ //
                            saveCptInfo();
                         }else if(m.type == "BTN_EDIT"){
                            showCptInfo(true,{capitalid:capitalid,viewtype:'edit'});
                         }
                    }}>
                    {m.menuName}
                </Button>
            );
        });
        btnArr.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@9wwy87@close`}  type="primary" onClick={()=>{contentStore.showCptInfo(false)}}>{getLabel(309,'关闭')}</Button>)  
        return btnArr;
    }
    getRightMenu(){
		const {contentStore} = this.props;
        const {cptInfo,} = this.props.contentStore;
        let btnArr = []; 
        let rightMenu = toJS(cptInfo.rightMenus) || [];
        rightMenu && rightMenu.length>0 && rightMenu.map(m=>{
            btnArr.push({
                icon: <i className={m.menuIcon} />,
                content: m.menuName
            })
        });
    	return btnArr
    }
    onRightMenuClick(key){
        const {contentStore,callBack} = this.props;
        const {cptInfo,saveCptInfo,capitalid,showCptInfo,onCptDel,doMend,onGiveback} = this.props.contentStore;
        let rightMenu = toJS(cptInfo.rightMenus) || [];
        let that = this;
        rightMenu &&  rightMenu.length>0  && rightMenu.map((m,i)=>{
        	if(Number(key) == i){
                if(m.type == "BTN_SUBMIT"){ //验收
                    saveCptInfo();
                }else if(m.type == 'BTN_BACK'){  //返回
                    showCptInfo(true,{capitalid:capitalid,viewtype:'view'});
                }else if(m.type == "BTN_EDIT"){
                    showCptInfo(true,{capitalid:capitalid,viewtype:'edit'});
                }else if(m.type == "BTN_DELETE"){
                    Modal.confirm({
                        title: getLabel(15172,'系统提示'),
                        content: getLabel(83600,'您确认要删除吗?'),
                        onOk() {
                            onCptDel({capitalid:capitalid,viewtype:'cptdel'},'list',that.updateLeftTree);
                            showCptInfo(false);
                        },
                        onCancel() {},
                    });
                }else if(m.type == "BTN_GIVEBACK"){  //归还
                    let params = {viewtype:'cptbackone',capitalid:capitalid}
                    Modal.confirm({
                        title:  getLabel(15172,'系统提示'),
                        content: getLabel(83559,'您确认要归还吗?'),
                        onOk() {
                            onGiveback(params,callBack);
                        },
                        onCancel() {},
                    });
                }else if(m.type == "BTN_MEND"){  //送修
                    doMend(true,{capitalid:capitalid});
                }else if (m.type == "BTN_LOG") {  //日志
                    contentStore.getModifyLog(capitalid);
                }
            }
        });
    }
    
    updateLeftTree = ()=>{
        const {contentStore} = this.props;
        contentStore.initTreeDatas();
    }
}
